/***********************************************
* Floating image script- By Virtual_Max (http://www.geocities.com/siliconvalley/lakes/8620)
* Modified by Dynamic Drive for various improvements
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/


var vmin = 2;
var vmax = 5;
var vr = 2;

function getBody() {
	if (window.innerWidth || window.opera) {
		return window;
	} else if (document.compatMode && document.compatMode != "BackCompat") {
		return document.documentElement;
	} else {
		return document.body;
	}
}

function Chip(chipname) {
	this.obj = document.getElementById(chipname);
	if(!this.obj) return;
	
	this.obj.style.zIndex=2000;
	this.named = chipname;
	this.vx = vmin + vmax * Math.random();
	this.vy = vmin + vmax * Math.random();
	this.w = this.obj.offsetWidth + 20;
	this.h = this.obj.offsetHeight;
	this.xx = 0;
	this.yy = 0;
	this.timer = null;

	this.movechip = function() {
		let b = getBody();
		pageX = 0;
		pageW = b.innerWidth - 40;
		pageY = 0;
		pageH = b.innerHeight - 20;
		
		this.xx = this.xx + this.vx;
		this.yy = this.yy + this.vy;
		
		this.vx += vr * (Math.random() - 0.5);
		this.vy += vr * (Math.random() - 0.5);
		if (this.vx > (vmax + vmin)) this.vx = (vmax + vmin) * 2 - this.vx;
		if (this.vx < (-vmax - vmin)) this.vx = (-vmax - vmin) * 2 - this.vx;
		if (this.vy > (vmax + vmin)) this.vy = (vmax + vmin) * 2 - this.vy;
		if (this.vy < (-vmax - vmin)) this.vy = (-vmax - vmin) * 2 - this.vy;

		if (this.xx <= pageX) {
			this.xx = pageX;
			this.vx = vmin + vmax * Math.random();
		}
		if (this.xx >= pageX + pageW - this.w) {
			this.xx = pageX + pageW - this.w;
			this.vx = -vmin - vmax * Math.random();
		}
		if (this.yy <= pageY) {
			this.yy = pageY;
			this.vy = vmin + vmax * Math.random();
		}
		if (this.yy >= pageY + pageH - this.h) {
			this.yy = pageY + pageH - this.h;
			this.vy = -vmin - vmax * Math.random();
		}

		document.getElementById(this.named).style.left = this.xx + "px";
		document.getElementById(this.named).style.top = this.yy + "px";
		
		this.timer = setTimeout(() => {this.movechip();}, 100);
	}
	
	this.movechip();
	
	this.obj.onmouseover = ()=>{ clearTimeout(this.timer); }
	this.obj.onmouseout = ()=>{ this.movechip(); }

}